//
//  BCBaseHttpManager.swift
//  BCProject
//
//  Created by you&me on 2017/11/24.
//  Copyright © 2017年 杨杨威. All rights reserved.
//

import UIKit

//网络请求类型
enum HttpType {
    case Get
    case Post
}

class BCBaseHttpManager: NSObject {
    
    typealias succeedCallBack = (_ result : Any) -> ()
    typealias failedCallBack = (_ result : Error?) -> ()
    
    static let share = BCBaseHttpManager()
    //MARK:-网络请求管理
    fileprivate var requestCacheArr = [DataRequest]()
    
    /*
     带有请求头的基础请求
     1.请求类型
     2.请求的url
     3.请求的参数
     4.请求头
     5.请求的返回
     */
     func requestHeaderData(_ type : HttpType, urlString : String, parameters : [String : Any]? = nil , header : [String : String]? = nil, finishedCallBack : @escaping succeedCallBack , failedCallBack :@escaping failedCallBack) {
        
        
        // 获取请求类型
        let httpType  = type == .Get ? HTTPMethod.get : HTTPMethod.post
        //获取请求URL
        let url =  urlString
        //发送网络请求

        let dataRequest = Alamofire.request(url, method: httpType, parameters: parameters , headers: header).responseJSON { (response) in
            //获取结果
            guard let result = response.result.value else {
                
                failedCallBack(response.result.error)
                
                return
            }
            
            finishedCallBack(result)
        }
         //记录网络请求
        self.requestCacheArr.append(dataRequest)
        
    }
    /*
     带有请求头的基础请求
     1.请求类型
     2.请求的url
     3.请求的参数
     5.请求的返回
     */
     func requestHYHeaderData(_ type : HttpType, urlString : String, parameters : [String : Any]? = nil ,finishedCallBack : @escaping succeedCallBack , failedCallBack :@escaping failedCallBack) {
        
        
        // 获取请求类型
        let httpType  = type == .Get ? HTTPMethod.get : HTTPMethod.post
        //获取请求URL
        let url =  urlString
        //设置 heander
        let Authorization = ""
        let header = ["Authorization":Authorization]
        //发送网络请求
       let dataRequest =  Alamofire.request(url, method: httpType, parameters: parameters , headers: header).responseJSON { (response) in
            //获取结果
            guard let result = response.result.value else {
                
                failedCallBack(response.result.error)
                
                return
            }
            finishedCallBack(result)
        }
        
        //记录网络请求
        self.requestCacheArr.append(dataRequest)
    }
    
    /*
     基础请求
     1.请求类型
     2.请求的url
     3.请求的参数
     4.请求头
     5.请求的返回
     */
     func requestData(_ type : HttpType, urlString : String, parameters : [String : Any]? = nil , finishedCallBack : @escaping succeedCallBack , failedCallBack :@escaping failedCallBack) {
        
        
        // 获取请求类型
        let httpType  = type == .Get ? HTTPMethod.get : HTTPMethod.post
        let headers: HTTPHeaders = [
            "Content-Type": "application/json; charset=utf-8"
        ]
        //获取请求URL
        let url =  urlString
        //发送网络请求
      let dataRequest =   Alamofire.request(url, method: httpType, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in


           print(response)
            //获取结果
            guard let result = response.result.value else {
                
                failedCallBack(response.result.error)
                
                return
            }
            finishedCallBack(result)
        }
        //记录网络请求
        self.requestCacheArr.append(dataRequest)
    }

   
   // 移除网络请求
    func cancleBCRequest()  {
        for task in requestCacheArr{
            task.cancel()
        }
        requestCacheArr.removeAll()
    }
    
}
